package com.sj.stone.shiro;

import com.sj.stone.domain.mysql.entry.AssUser;
import com.sj.stone.service.AssUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class MyRealm extends AuthorizingRealm {
	private static final Logger LOG = LoggerFactory.getLogger(MyRealm.class);
	@Autowired
	private AssUserService assUserService;

	public MyRealm(AssUserService assUserService) {
		super();
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		return token instanceof UsernamePasswordToken;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//		System.out.println("身份认证方法：MyShiroRealm.doGetAuthenticationInfo()");
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		String username = upToken.getUsername();
		String password = upToken.getPassword().toString();
		AssUser assUser = assUserService.getByUsername(username);
		List<AssUser> userList = assUserService.getByUsernameAndPwd(username,password);
		if (userList.size() != 0) {
			assUser = userList.get(0);
			System.out.println(assUser.getNickname());
		}else if (null == assUser) {
            throw new AccountException("帐号或密码不正确！");
        }
        return new SimpleAuthenticationInfo(assUser.getUsername(), assUser.getPassword(), getName());

	}

	/**
	 * 登陆成功之后，进行角色和权限验证
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//		User user = (User) principals.fromRealm(getName()).iterator().next();
//		LOG.info("USER===="+user.getRoles());
//		if (user.getUsername().equals("root")) {
//			// 超级管理员
//			authorizationInfo.addRole("admin");
//			authorizationInfo.addStringPermission("*");
//		} else {
//			Collection<String> roles = userService.getRolesByUsername(user.getUsername());
//			LOG.debug("{}'s roles are {}",user.getUsername(),roles);
//			authorizationInfo.addRoles(roles);
//			Collection<String> permissions = userService.getPermissionsByUsername(user.getUsername());
//			LOG.debug("{}'s permissions are {}",user.getUsername(),permissions);
//			authorizationInfo.addStringPermissions(permissions);
//		}
//
//		return authorizationInfo;
        return null;
	}
	/**
	 * 清除所有用户授权信息缓存.
	 */
	public void clearCachedAuthorizationInfo(String principal) {
		SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
		clearCachedAuthorizationInfo(principals);
	}


	/**
	 * 清除所有用户授权信息缓存.
	 */
	public void clearAllCachedAuthorizationInfo() {
		Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
		if (cache != null) {
			for (Object key : cache.keys()) {
				cache.remove(key);
			}
		}
	}

}